import {Modal, Tag} from 'antd';
import React, {useRef} from 'react';
import {PageContainer} from '@ant-design/pro-layout';
import {isSuccess} from "@/utils/utils";
import {DesignerManager} from "@/services/api-functions";
import ProTable, {ActionType, ProColumns} from "@ant-design/pro-table";
import {ExclamationCircleOutlined} from "@ant-design/icons";
import {SYS_ENUMS} from "@/utils/constants";

export const caseRecommend = async (select: any, recommend: boolean) => {
  if (!select) return false;
  try {
    if (isSuccess((await DesignerManager.serverList1(select.replyUserDto.userId, recommend))?.code)) {
      return true;
    }
    return false
  } catch (error) {
    return false;
  }
};


const ServiceList: React.FC<any> = ({}) => {
  const actionRef = useRef<ActionType>();
  const columns: ProColumns[] = [
    {
      title: '设计师',
      dataIndex: 'designerName',
      ellipsis: true,
      hideInTable: true,
    },
    {
      title: '服务名称',
      dataIndex: 'serverName',
      ellipsis: true,
      fixed: 'left',
      width: 350,
    },
    {
      title: '设计师',
      dataIndex: ['replyUserDto', 'nickName'],
      ellipsis: true,
      hideInSearch: true,
      width: 120,
    },
    {
      title: '设计师等级',
      dataIndex: 'customizeLevel',
      ellipsis: true,
      hideInSearch: true,
      valueEnum: SYS_ENUMS.LEVEL,
      width: 150,
    },
    {
      title: '是否推荐',
      dataIndex: 'recommend',
      valueEnum: {
        true: {text: '是', status: 'Success'},
        false: {text: '否', status: 'Error'},
      },
      width: 100,
      hideInSearch: true,
      hideInForm: true,
    },
    {
      title: '基础服务',
      dataIndex: 'priceBase',
      ellipsis: true,
      hideInSearch: true,
      width: 150,
    },
    {
      title: '标准服务',
      dataIndex: 'priceStandard',
      ellipsis: true,
      hideInSearch: true,
      width: 150,
    },
    {
      title: '高级服务',
      dataIndex: 'priceAdvanced',
      ellipsis: true,
      hideInSearch: true,
      width: 150,
    },
    {
      title: '主页标签',
      dataIndex: 'labels',
      ellipsis: true,
      width: 150,
    },
    {
      title: '服务订单数',
      dataIndex: 'serverTimes',
      width: 100,
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '服务评分',
      dataIndex: 'serverScore',
      width: 100,
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '评价数',
      dataIndex: 'serverTimes',
      width: 100,
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '今日访客',
      dataIndex: 'todayVisitor',
      width: 100,
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '收藏数',
      dataIndex: 'focusNum',
      width: 100,
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '最后编辑时间',
      dataIndex: 'gmtModified',
      valueType: 'dateTime',
      width: 170,
      hideInSearch: true,
      hideInForm: true,
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      hideInSearch: true,
      fixed: 'right',
      hideInForm: true,
      render: (_, record) => [
        <a key="edit"
           href={`/order/service/list?id=${record?.id}`}
        >订单</a>,
        // <a key="edit" href={`/article/detail/${record?.id}`}>详情</a>,
        (
          record.recommend
            ? <span style={{color: 'gray'}}>推荐</span>
            : <a key="edit" onClick={() => Modal.confirm({
              title: '提示',
              icon: <ExclamationCircleOutlined/>,
              content: <span>确定要推荐 <Tag color='#108ee9'>{record.serverName}</Tag> 吗？</span>,
              okText: '推荐',
              cancelText: '取消',
              onOk: async () => {
                if (await caseRecommend(record, true)) {
                  actionRef.current?.reload()
                }
              },
            })}>推荐</a>
        ),
      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable<any, any>
        actionRef={actionRef}
        rowKey="key"
        search={{
          labelWidth: 120,
        }}
        request={async (params) => {

          const {data: {data, total}, code}: any = await DesignerManager.serverList(params);

          return {data: data || [], success: isSuccess(code), total: isSuccess(code) ? total : 0};
        }}
        columns={columns}
        scroll={{ x: 1300 }}
      />
    </PageContainer>
  );
};

export default ServiceList;
